package com.cryptooj.project.judge.strategy;

import cn.hutool.json.JSONUtil;
import com.cryptooj.project.judge.codesandbox.model.JudgeInfo;
import com.cryptooj.project.model.dto.question.JudgeCase;
import com.cryptooj.project.model.dto.question.JudgeConfig;
import com.cryptooj.project.model.entity.Question;
import com.cryptooj.project.model.entity.QuestionSubmit;
import com.cryptooj.project.model.enums.JudgeInfoMessageEnum;

import java.util.List;

/**
 *  默认判题策略
 * */
public class DefaultJudgeStrategy implements JudgeStrategy{

    /**
     *  执行判题
     * */
    @Override
    public JudgeInfo doJudge(JudgeContext judgeContext) {
        JudgeInfo judgeInfo = judgeContext.getJudgeInfo();
        QuestionSubmit questionSubmit = judgeContext.getQuestionSubmit();

        Long memory = judgeInfo.getMemory();
        Long time = judgeInfo.getTime();
        List<String> inputList = judgeContext.getInputList();
        List<String> outputList = judgeContext.getOutputList();
        List<JudgeCase> judgeCaseList = judgeContext.getJudgeCaseList();
        Question question = judgeContext.getQuestion();

        JudgeInfoMessageEnum judgeInfoMessageEnum = JudgeInfoMessageEnum.ACCEPTED;
        JudgeInfo judgeInfoResponse = new JudgeInfo();
        judgeInfoResponse.setMemory(memory);
        judgeInfoResponse.setTime(time);
        // 判断沙箱执行结果的输出数量是否与预期输出数量相等
        if(outputList.size() != inputList.size()){ // 一组输入对应一组输出
            // 作答错误
            judgeInfoMessageEnum = judgeInfoMessageEnum.WRONG_ANSWER;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }
        // 判断每一项输出与预期输出是否相等
        for (int i = 0; i <judgeCaseList.size();i++){
            JudgeCase judgeCase = judgeCaseList.get(i);
            if(!judgeCase.getOutput().equals(outputList.get(i))){
                // 作答错误
                judgeInfoMessageEnum = judgeInfoMessageEnum.WRONG_ANSWER;
                judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
                return judgeInfoResponse;
            }
        }
        // 判断作答结果是否超出限制
        String judgeConfigStr = question.getJudgeConfig();
        JudgeConfig judgeConfig = JSONUtil.toBean(judgeConfigStr, JudgeConfig.class);
        Long timeLimit = judgeConfig.getTimeLimit();
        Long memoryLimit = judgeConfig.getMemoryLimit();
        Long stackLimit = judgeConfig.getStackLimit();
        if(memory > memoryLimit){
            // 作答正确，但是超限
            judgeInfoMessageEnum = JudgeInfoMessageEnum.MEMORY_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }
        if(time > timeLimit){
            // 作答正确，但是超限
            judgeInfoMessageEnum = JudgeInfoMessageEnum.TIME_LIMIT_EXCEEDED;
            judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
            return judgeInfoResponse;
        }
        // if(stack > stackLimit){}

        // 作答正确
        judgeInfoResponse.setMessage(judgeInfoMessageEnum.getValue());
        return judgeInfoResponse;
    }
}
